package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.FileSendDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.FileSend;
import com.prs.crm.domain.oa.FileSendAccept;
import com.prs.crm.domain.oa.NoticeAccept;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.FileSendAcceptService;
import com.prs.crm.service.oa.FileSendService;
import com.prs.crm.service.oa.NoticeAcceptService;

public class FileSendServiceImpl extends BaseServiceImpl<FileSend, FileSendDao> implements FileSendService{

	private PersonService personService;
	private FileSendAcceptService fileSendAcceptService;
	
	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public FileSendAcceptService getFileSendAcceptService() {
		return fileSendAcceptService;
	}

	public void setFileSendAcceptService(FileSendAcceptService fileSendAcceptService) {
		this.fileSendAcceptService = fileSendAcceptService;
	}

	@Override
	public void saveFileSend(FileSend fileSend, File file, String fileName,
			String accepts) {
		// TODO Auto-generated method stub
		fileSend.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(fileSend);
		
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			FileSendAccept a = new FileSendAccept();
			a.setFileSend(fileSend);
			a.setPerson(person);
			a.setState(Constrains.NOTICE_UNREAD);
			
			this.getFileSendAcceptService().save(a);
		}
	}

	@Override
	public void saveFileSendToCaogao(FileSend fileSend, File file,
			String fileName) {
		// TODO Auto-generated method stub
		fileSend.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(fileSend);
	}

}
